var ns;
var app = new Vue({
  el: '#app',
  data: {
    userChildList: [],
    childUserId: '',
    // userList:[]
    isShowid: 0,
    selectUserid: 0,
    selectUserName: '',
    showreset: false,//是否打开重置密码
    showdisplay: false,//是否禁用账户
    showdelete: false,//是否删除账号
  },
  methods: {
    get_data: function () {
      var that = this;
      ns.post({
        action: 'getuserlist'
      }, function (succeed, data, err) {
        console.log(JSON.stringify(data));
        if (succeed) {
          if (data.status) {
            that.userChildList = data.MemberList;
          }

        } else {
          api.toast({
            msg: err,
            duration: 2000,
            location: 'bottom'
          });
        }

      })



    },
    open_addzh: function (item) {
      var that = this;
      ns.openWin('dl_addzhanghu', '新增账户');
    },
    do_opencaozuo: function (id) {
      var that = this;
      that.isShowid = id;
    },
    closebox: function () {
      var that = this;
      that.isShowid = 0;
    },
    // 授权
    shouquan: function (id) {
      var that = this;
      that.isShowid = 0;
      console.log(id);
      ns.openWin('zhanghu_shouquan', '账户授权', {
        id: id
      });
    },
    // 重置密码
    reset_password: function (id, name) {
      var that = this;
      that.selectUserid = id;
      that.selectUserName = name;
      that.isShowid = 0;
      that.showreset = true;
    },
    beforereset_password: function (action, done) {
      var that = this;
      if (action === 'confirm') {
        ns.post({
          action: 'resetpassword',
          UserID: that.selectUserid
        }, function (succeed, data, err) {
          if (succeed) {
            if (data.status) {
              api.toast({
                msg: data.msg,
                duration: 2000,
                location: 'bottom'
              });
              setTimeout(function () {
                ns.openWin('login_frm', '',);
              }, 200);
            } else {
              api.toast({
                msg: data.msg,
                duration: 2000,
                location: 'bottom'
              });
            }

          } else {
            api.toast({
              msg: err,
              duration: 2000,
              location: 'bottom'
            });
          }
        });
        that.showreset = false;

        done();
      } else if (action === 'cancel') {
        done();
      }
    },
    //编辑
    edit_zhanghao: function (item) {
      var that = this;
      console.log(JSON.stringify(item));
      that.isShowid = 0;
      ns.openWin('zhanghu_edit', '账号编辑', {
        item: item
      });
    },
    // 禁用
    display_zhanghao: function (id) {
      var that = this;
      that.isShowid = 0;
      that.selectUserid = id;
      that.showdisplay = true;
    },
    beforedisplay_zhanghao: function (action, done) {
      var that = this;
      if (action === 'confirm') {
        ns.post({
          action: 'userstop',
          UserID: that.selectUserid
        }, function (succeed, data, err) {
          console.log(JSON.stringify(err));
          console.log(JSON.stringify(data));
          if (succeed) {
              api.toast({
                msg: '已禁用',
                duration: 2000,
                location: 'bottom'
              });
              that.get_data();
          }else{
            api.toast({
              msg: err,
              duration: 2000,
              location: 'bottom'
            });
          }
        })
        that.showdisplay = false;
        done();
      } else if (action === 'cancel') {
        done();
      }
    },
    //启用
    open_zhanghao:function(id){
      var that=this;
      that.isShowid = 0;
      ns.post({
        action: 'useruse',
        UserID: id
      }, function (succeed, data, err) {
        if (succeed) {
            api.toast({
              msg: '已启用',
              duration: 2000,
              location: 'bottom'
            });
            that.get_data();
        }else{
          api.toast({
            msg: err,
            duration: 2000,
            location: 'bottom'
          });
        }
      })
     
    },
      // 删除
      delete_zhanghao: function(id) {
        var that = this;
        that.isShowid = 0;
        that.selectUserid = id;
        that.showdelete = true;
      },
      beforedelete_zhanghao: function(action, done) {
        var that = this;
        if (action === 'confirm') {
          ns.post({
            action: 'deleteuser',
            UserID: that.selectUserid
          }, function (succeed, data, err) {
            if(succeed){
              if(data.status){
                api.toast({
                  msg: data.msg,
                  duration: 2000,
                  location: 'bottom'
                });
                that.get_data();
              }else{
                api.toast({
                  msg: data.msg,
                  duration: 2000,
                  location: 'bottom'
                });
              }
            }else{
              api.toast({
                msg: err,
                duration: 2000,
                location: 'bottom'
              });
            }
          });
          that.showdelete = false;
          done();
        } else if (action === 'cancel') {
          done();
        }
      },



    },






  });
apiready = function () {
  api.parseTapmode();
  ns = window.Foresight.Util;
  app.childUserId = api.pageParam.id;
  app.get_data();
  api.addEventListener({
    name: 'load_zhanghao'
  }, function (ret) {
    app.get_data();
  })
};
